/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.jdbc.core.RowMapper;
import pl.cezary.butler.walili.server.shuffling.Shufflable;


/**
 *
 * @author Czarek
 */
public class DownloadTask extends Shufflable{
    private int id;
    private URL url;
    private DownloadTaskStatus status;

    public DownloadTask(URL url, DownloadTaskStatus status) {
        this.url = url;
        this.status = status;
    }

    public DownloadTaskStatus getStatus() {
        return status;
    }

    public URL getUrl() {
        return url;
    }
    
    public static RowMapper<DownloadTask> getMapper(){
        return new RowMapper<DownloadTask>() {

            @Override
            public DownloadTask mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    final DownloadTask result = new DownloadTask(new URL(rs.getString("url")), DownloadTaskStatus.values()[rs.getInt("status")]);
                    result.id=rs.getInt("id");
                    return result;
                } catch (MalformedURLException ex) {
                    Logger.getLogger(DownloadTask.class.getName()).log(Level.SEVERE, null, ex);
                }
                return null;//should never happen;
            }
        };
    }

    public void setStatus(DownloadTaskStatus status) {
        this.status = status;
    }
    
    
}
